﻿package cn.poco.pepsi.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
public class UrlConnectionUtil {

/*	public static InputStream getRequest(String path) throws Exception{
		URL url = new URL(path);
	    HttpURLConnection conn = (HttpURLConnection)url.openConnection();	    
	    conn.setConnectTimeout(10 * 3000);
	    conn.setReadTimeout(10 * 3000);
	    conn.setRequestMethod("GET");
	    int code = conn.getResponseCode();
		if(code==200){
			InputStream inStream = conn.getInputStream();
			return inStream;
		}else{
			throw new Exception("conn exception");
		}
	}*/
	
	
	public static InputStream getRequest(String path) throws Exception{
		InputStream inStream = null;
		HttpGet httpRequest = new HttpGet(path);
		HttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 6000);
		HttpResponse httpResponse = httpClient.execute(httpRequest);
		if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
			inStream = httpResponse.getEntity().getContent();
		}else{
			throw new Exception("conn exception");
		}
		
		return inStream;
	}
	
}
